package com.yangzhe.algorithm.c035;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Map;

// setAll功能的哈希表
// 测试链接 : https://www.nowcoder.com/practice/7c4559f138e74ceb9ba57d76fd169967
public class Code01_SetAllHashMap_Acm {

    public static void main(String[] args) throws IOException {
        try (
                InputStreamReader input = new InputStreamReader(System.in);
                BufferedReader reader = new BufferedReader(input);
        ) {
            StreamTokenizer token = new StreamTokenizer(reader);
            token.nextToken();
            int optCount = (int) token.nval;
            for (int i = 0; i < optCount; i++) {
                token.nextToken();
                int optType = (int) token.nval;

                // 如果是put
                if (optType == 1) {
                    token.nextToken();
                    int key = (int) token.nval;

                    token.nextToken();
                    int value = (int) token.nval;

                    put(key, value);
                } else if (optType == 2) {
                    token.nextToken();
                    int key = (int) token.nval;
                    System.out.println(get(key) + "");
                } else if (optType == 3) {
                    token.nextToken();
                    int setAllValue = (int) token.nval;
                    setAll(setAllValue);
                }
            }

        }
    }

    /**
     * 记录setAll的值和当时Map操作次数的检查点
     */
    static int setAllValue;
    static int setAllPoint;

    /**
     * 记录当前Map操作的次数
     */
    static int currentPoint = 0;

    /**
     * 记录map，value扩展成数组，0位置是数字，1位置是数组操作的次数
     * 这种设计数组操作次数有限，但是能过题目用例
     */
    static Map<Integer, int[]> map = new HashMap<>();

    static void put(int key, int v) {
        int value[];
        if (map.containsKey(key)) {
            value = map.get(key);
        } else {
            value = new int[2];
        }
        value[0] = v;
        // 记录put时map操作的次数
        value[1] = currentPoint++;

        map.put(key, value);
    }

    static void setAll(int value) {
        // setAll 时操作次数也要加，否则最后一次put和setAll的操作次数就相等了
        setAllPoint = currentPoint++;
        setAllValue = value;
    }

    static int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }

        int value[] = map.get(key);

        // 如果数值put时操作次数大于setAll检查点的次数，则代表这个数字时setAll之后put的，返回原来的值即可
        if (value[1] > setAllPoint) {
            return value[0];
        } else {
            return setAllValue;
        }
    }
}
